package com.xyx.yfy_site_msg.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyx.yfy_site_msg.system.entity.YfyUpdate;
import com.xyx.yfy_site_msg.system.entity.YfyUser;
import com.xyx.yfy_site_msg.system.service.YfyUpdateService;
import com.xyx.yfy_site_msg.system.utils.DataGridView;
import com.xyx.yfy_site_msg.system.utils.ResultObj;
import com.xyx.yfy_site_msg.system.utils.WebUtils;
import com.xyx.yfy_site_msg.system.vo.YfyUpdateVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyx
 * @since 2021-01-13
 */
@RestController
@RequestMapping("/update")
public class YfyUpdateController {

    @Autowired
    private YfyUpdateService updateService;

    /**
     * 分页查询升级记录
     *
     * @param updateVo
     * @return
     */
    @RequestMapping("loadAllUpdateData")
    public DataGridView loadAllUpdateData(YfyUpdateVo updateVo) {
        IPage<YfyUpdate> page = new Page<>(updateVo.getPage(), updateVo.getLimit());
        QueryWrapper<YfyUpdate> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(updateVo.getCustomer()), "customer", updateVo.getCustomer());
        queryWrapper.like(StringUtils.isNotBlank(updateVo.getPerson()), "person", updateVo.getPerson());
        queryWrapper.like(StringUtils.isNotBlank(updateVo.getAddress()), "address", updateVo.getAddress());
        queryWrapper.ge(updateVo.getUpdateStartTime() != null, "updatetime", updateVo.getUpdateStartTime());
        queryWrapper.le(updateVo.getUpdateEndTime() != null, "updatetime", updateVo.getUpdateEndTime());
        queryWrapper.ge(updateVo.getCreateStartTime() != null, "createtime", updateVo.getCreateStartTime());
        queryWrapper.le(updateVo.getCreateEndTime() != null, "createtime", updateVo.getCreateEndTime());
        queryWrapper.orderByDesc("id");
        this.updateService.page(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 添加升级记录
     *
     * @param updateVo
     * @return
     */
    @RequestMapping("addUpdate")
    public ResultObj addUpdate(YfyUpdateVo updateVo) {
        try {
            YfyUser user = (YfyUser) WebUtils.getSession().getAttribute("user");
            updateVo.setCreatebody(user.getUsername());
            updateVo.setCreatetime(new Date());
            this.updateService.save(updateVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新升级记录
     *
     * @param updateVo
     * @return
     */
    @RequestMapping("updateData")
    public ResultObj updateData(YfyUpdateVo updateVo) {
        try {
            YfyUser user = (YfyUser) WebUtils.getSession().getAttribute("user");
            updateVo.setSolvebody(user.getUsername());
            updateVo.setSolvetime(new Date());
            this.updateService.updateById(updateVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除一条升级记录
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteUpdate")
    public ResultObj deleteUpdate(Integer id) {
        try {
            this.updateService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_SUCCESS;
        }
    }

    /**
     * 批量删除升级记录
     *
     * @param ids
     * @return
     */
    @RequestMapping("batchDeleteUpdate")
    public ResultObj batchDeleteUpdate(Integer[] ids) {
        try {
            Collection<Serializable> list = new ArrayList<>();
            for (Integer id : ids) {
                list.add(id);
            }
            updateService.removeByIds(list);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

